<?php

namespace App\Dao;

use App\Model\Banner;
use App\Utils\FunUtil;
use Hyperf\Context\ApplicationContext;

class BannerDao
{

    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }


    /**
     * 页面banner数据
     * @param $from
     * @param $version_code
     * @param $sub_type
     * @param array $user
     * @param string $package
     * @param int $limit
     * @param string $goddess_rank
     * @return mixed
     */
    public function getOnlineList($from, $version_code, $sub_type, $user = [], $package = '', $limit = 5, $goddess_rank = '', $channel = '')
    {
        // $list = Mem::get("online_banner_list_" . $from . "_{$package}_{$sub_type}_" . $version_code);
        $list = false;
        if ($list === false) {
            $time = time();
            $query = Banner::query()->where("start_time", "<", $time)->where("end_time", ">", $time)->where("status", 1)->where("version_min", "<=", $version_code)->where("version_max", ">=", $version_code)->where("sub_type", $sub_type);

            if ((new AppUpgradeDao())->isHideCall($from, $version_code, $channel)) {
                $query->whereNotIn('id', [54, 53, 57, 65]);
            }
            if (!empty($user)) {
                $query->where(function ($q) use ($user) {
                    $q->where('gender', 0)->orWhere('gender', $user['sex']);
                });
            } else {
                $query->where('gender', 0);
            }

            if ($package) {
                $query->where(function ($q) use ($package) {
                    $q->whereRaw("FIND_IN_SET('{$package}', package)")->orWhere('package', '');
                });
            }

            if (empty($goddess_rank)) {
                $query->where('title', '<>', '女神排行榜');
            }

            $query->where(function ($q) use ($from) {
                $q->where('type', 0)->orWhere('type', $from);
            });

            $list = $query->orderBy('pos', 'ASC')
                ->orderBy('id', 'DESC')
                ->take($limit)
                ->get();
            if (!empty($list)) {
                foreach ($list as $k => $v) {
                    $list[$k]['img'] = FunUtil::formatPhoto($v['img']);
                    unset($list[$k]['start_time']);
                    unset($list[$k]['end_time']);
                    unset($list[$k]['status']);
                    unset($list[$k]['pos']);
                    unset($list[$k]['type']);
                    unset($list[$k]['version_min']);
                    unset($list[$k]['version_max']);
                    unset($list[$k]['sub_type']);
                    unset($list[$k]['is_new_user']);
                    if ($from == 1) {
                        $list[$k]['link'] = base64_encode($v['link']);
                    } elseif ($from == 2) {
                        //ios时,因为客户端写死了link_ios字段，所以这里不能用link字段
                        $list[$k]['link_ios'] = base64_encode($v['link']);
                        unset($list[$k]['link']);
                    }
                }
            }
            // Mem::set("online_banner_list_" . $from . "_{$package}_{$sub_type}_" . $version_code, $list, 30);
        }

        return $list;
    }


}